package com.jams.goodox.activity.serve.enterprise;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.adapter.CustomFragmentPagerAdapter;
import com.jams.goodox.entity.resp.FinancialDetailsResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.permission.PhoneUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

import static com.jams.goodox.activity.serve.enterprise.EnterpriseFinancialActivity.MANAGE_PRODUCT_ID;

/**
 * 理财详情
 */
public class EnterpriseFinancialDetailActivity extends BaseActivity {
    @Bind(R.id.sdv_bank_icon)
    SimpleDraweeView mSdvBankIcon;
    @Bind(R.id.tv_department_name)
    TextView mTvDepartmentName;
    @Bind(R.id.tv_tel)
    TextView mTvTel;
    @Bind(R.id.tv_department_address)
    TextView mTvDepartmentAddress;
    @Bind(R.id.tv_phone)
    TextView mTvPhone;
    @Bind(R.id.tabs_loan)
    TabLayout mTabsLoan;
    @Bind(R.id.vp_loan)
    ViewPager mVpLoan;
    private String productId;
    private List<String> tittleList = new ArrayList<>();
    private List<Fragment> contentList = new ArrayList<>();
    private FragmentPagerAdapter mAdapter;
    private String phone ="";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_loan_detail;
    }

    @Override
    protected void init() {
        productId = StringUtils.newString(getIntent().getStringExtra(MANAGE_PRODUCT_ID));

        StyleUtil.titleBackKey(this, getResources().getString(R.string.finance_detail));
        getFinancialData();
    }

    @OnClick(R.id.tv_phone)
    public void onViewClicked() {
        new PhoneUtils(mContext).makeCall(StringUtils.newString(""), StringUtils.newString(phone));
    }

    /**
     * 获取理财数据
     */
    private  void getFinancialData(){
        showLoading();
        enqueue(createCallService(EnterpriseService.class).getFinanceProduct(productId), new RetrofitCallbackListener<FinancialDetailsResp>() {
            @Override
            public void onSuccess(Call<FinancialDetailsResp> call, FinancialDetailsResp result) {
                misLoading();
                try {
                    mTvPhone.setVisibility(View.VISIBLE);
                    phone = StringUtils.newString(result.data.connection);
                    mTvTel.setText("TEL:"+result.data.connection);
                    mSdvBankIcon.setImageURI(StringUtils.newString(result.data.icon));
                    mTvDepartmentName.setText(result.data.bankName);
                    mTvDepartmentAddress.setText(result.data.address);
                    tittleList.add(getResources().getString(R.string.steady_finance));
                    tittleList.add(getResources().getString(R.string.cautious_finance));
                    contentList.add(EnterprisePolicyDetailFragment.newInstance(StringUtils.newString(result.data.stable)));
                    contentList.add(EnterprisePolicyDetailFragment.newInstance(StringUtils.newString(result.data.careful)));
                    mAdapter = new CustomFragmentPagerAdapter(getSupportFragmentManager(), tittleList, contentList);
                    mVpLoan.setAdapter(mAdapter);
                    mTabsLoan.setupWithViewPager(mVpLoan);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String msg) {
                misLoading();
            }
        });
    }
}
